home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 687 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: 05 Jan 1996 20:58:30 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan5125830@more.eng.sun.com>
  8. References: <30ECA10F.3D99@ifu.net>
  9. NNTP-Posting-Host: more.eng.sun.com
  10. In-reply-to: Jason Gresh's message of Thu, 04 Jan 1996 22:54:55 -0500
  11.  
  12. In article <30ECA10F.3D99@ifu.net> Jason Gresh <gresh@ifu.net> writes:
  13.  
  14. >   From: Jason Gresh <gresh@ifu.net>
  15. >   Newsgroups: comp.lang.c++
  16. >   Date: Thu, 04 Jan 1996 22:54:55 -0500
  17. >   Organization: Internet For 'U'
  18. >
  19. >   Hi,
  20. >
  21. >       I am trying to create a base class that has a function that the 
  22. >   derived class needs to create.  In order to do this, I want to create a 
  23. >   pointer to a function in the base class that the derived class can then 
  24. >   define.  This is not a case where an override will work because the 
  25. >   number and names of the derived functions is not known.  Hopefully this 
  26. >   code sample will make this clearer:
  27. >
  28. >   class BaseClass
  29. >   {
  30. >       int (*ptrFunction)();
  31. >   }
  32. >
  33. >   class DerivedClass : BaseClass
  34. >   {
  35. >       DerivedClass::DerivedClass();
  36. >       int myFunction(int, int);
  37. >   }
  38. >
  39. >   DerivedClass::DerivedClass()
  40. >   {
  41. >       ptrFunction = myFunction;
  42. >   }
  43. >
  44. >   DerivedClass::myFunction(int, int)
  45. >   {
  46. >   // code ....
  47. >   }
  48. >
  49. >   If I don't make myFunction part of the derived class (global), 
  50. >   everything works fine.  As soon as I include it in the class, I cannot 
  51. >   assign a pointer to it.  I am aware that pointers to functions inside 
  52. >   the class include the class name in some way ( this is not an issue for 
  53. >   global functions).  What is the casting (or other) mechanism to make
  54. >   this work?
  55. >
  56. >   Thanks,
  57. >
  58. >       Mike Gresh
  59. >
  60.  
  61. I feel you are trying to recreate what C++ already provides you,
  62. which is virtual methods (or even pure virtual methods).  You can define a
  63. function in your BaseClass as virtual and provide default code for it or
  64. define it as pure virtual and enforce that *all* derived classes provide code
  65. for it.  If you are using virtual functions, the signature of the method in
  66. the base class as well as in all derived classes must be same.  
  67.  
  68. If you are unfamiliar about the virtual methods, please read your C++ book.
  69. Let me know if you need more help with this or if I misunderstood your
  70. question. 
  71.  
  72. -Nitin
  73. -- 
  74. ----------------------------------------------------------------------
  75. Nitin More                                                         
  76. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  77. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  78. ----------------------------------------------------------------------
  79.